/**
 * Created by Cake on 2017/6/5.
 */

$(function () {

    var domProject = $('.project');
    if (domProject.length > 0) {
        domProject.mobiscroll().select({
            theme: 'mobiscroll',
            mode: 'scroller',
            display: 'bottom',
            lang: 'zh',
            placeholder: '请选择所属项目',
            headerText:"所属项目",
            onInit: function() {
                domProject
                    .val('')
                    .prev().val('');
            }
        });
    }

    var domProvince = $('.province');
    if (domProvince.length > 0) {
        domProvince.mobiscroll().select({
            theme: 'mobiscroll',
            mode: 'scroller',
            display: 'bottom',
            lang: 'zh',
            placeholder: '请选择所在省',
            headerText:"所在省",
            onInit: function() {
                domProvince
                    .val('')
                    .prev().val('');
            }
        });
    }

    var domCity = $('.city');
    if (domCity.length > 0) {
        domCity.mobiscroll().select({
            theme: 'mobiscroll',
            mode: 'scroller',
            display: 'bottom',
            lang: 'zh',
            placeholder: '请选择所在市',
            headerText:"所在市",
            onInit: function() {
                domCity
                    .val('')
                    .prev().val('');
            }
        });
    }

    var domArea = $('.area');
    if (domArea.length > 0) {
        domArea.mobiscroll().select({
            theme: 'mobiscroll',
            mode: 'scroller',
            display: 'bottom',
            lang: 'zh',
            placeholder: '请选择所在区',
            headerText:"所在区",
            onInit: function() {
                domArea
                    .val('')
                    .prev().val('');
            }
        });
    }

    $('.order-list-price input')
        .focus(function () {
            $(this)
                .css({
                    'border-bottom': '1px solid #2e82ff'
                })
                .prev().css({
                    'color': '#2e82ff'
                });
        })
        .blur(function () {
            if ($(this).val() == '') {
                $(this)
                    .css({
                        'border-bottom': '1px solid #8c8c8c'
                    })
                    .prev().css({
                    'color': '#8c8c8c'
                });
            }

        });


    //数量控制
    $('.num-btn').click(function(){
        var numVar = $(this).parent().find('input');
        var numValue = numVar.val();
        var num = parseInt(numValue);
        if ($(this).hasClass('add')) {
            if (numValue < 9999) {
                num++;
            }
        } else {
            if (numValue > 1) {
                num--;
            }
        }
        numVar.val(num);
    });

    //输入控制
    $('.number input').bind('input propertychange', function() {

        var _this = $(this);
        var thisVal = $.trim(_this.val());

        _this.val(thisVal);
        $.trim(_this.val($.trim(thisVal).replace(/\D/g,'')));

        if ($.trim(thisVal) == '') {
            return false;
        } else if ($.trim(thisVal) == 0) {
            _this.val('1');
        }

        if ($.trim(thisVal) > 9999 ) {
            _this.val(9999);
        }

    }).bind('paste',function(){

        var _this = $(this);
        var thisVal = $.trim(_this.val());

        _this.val(thisVal);
        $.trim(_this.val($.trim(thisVal).replace(/\D/g,'')));

        if ($.trim(thisVal) == '') {
            return false;
        } else if ($.trim(thisVal) == 0) {
            _this.val('1');
        }

        if ($.trim(thisVal) > 9999 ) {
            _this.val('9999');
        }

    }).css('ime-mode', 'disabled');

    $('.number input').blur(function () {
        if ($.trim($(this).val()) == '') {
            $(this).val('1');
        }
    });

    totalPrice();

    $('.num-btn').click(function () {
        totalPrice();
    });

    $('.number input').on('input propertychange', function () {
        totalPrice();
    })

    $('.order-list-price input').on('input propertychange', function () {
        totalPrice();

        clearNoNum(this);
    })
});

function totalPrice () {

    var singlePrice,
        singleNum,
        singleTotal,
        arr = [];

    arr.splice(0,arr.length);

    $('.order-list li').each(function () {

        if ($(this).find('.order-list-price input').val() != '') {
            singlePrice = parseFloat($(this).find('.order-list-price input').val());
        } else {
            singlePrice = 0;
        }

        if ($(this).find('.number input').val() != '') {
            singleNum = parseFloat($(this).find('.number input').val());
        } else {
            singleNum = 1;
        }

        singleTotal = singlePrice * singleNum;

        arr.push(singleTotal);

    });

    console.log(arr);

    var sum = 0;

    for (var i = 0; i < arr.length; i++){
        sum += parseFloat(arr[i]);
    }

    sum = sum.toFixed(2);

    $('.order-list-bottom p em').html(sum);
    $('.store-opt p').html('￥' + sum);

}

/* 只能输入数字、一个小数点和2位小数 */
function clearNoNum(obj){
    obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
}

